package io.renren.modules.wms.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.renren.common.utils.R;
import io.renren.modules.wms.entity.WeiboContentEntity;
import io.renren.modules.wms.entity.WeiboPicEntity;
import io.renren.modules.wms.service.WeiboContentService;
import io.renren.modules.wms.service.WeiboPicService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.wms.dao.PictureInfoDao;
import io.renren.modules.wms.entity.PictureInfoEntity;
import io.renren.modules.wms.service.PictureInfoService;

import javax.annotation.Resource;


@Service("pictureInfoService")
public class PictureInfoServiceImpl extends ServiceImpl<PictureInfoDao, PictureInfoEntity> implements PictureInfoService {

    @Resource
    private WeiboContentService weiboContentService;

    @Resource
    private WeiboPicService weiboPicService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PictureInfoEntity> page = this.page(
                new Query<PictureInfoEntity>().getPage(params),
                new QueryWrapper<PictureInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<PictureInfoEntity> queryByWeiboId(Integer weiboId) {
        QueryWrapper<PictureInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(PictureInfoEntity::getWeiboId, weiboId);
        List<PictureInfoEntity> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public R saveWeiBoPic(PictureInfoEntity pictureInfoEntity) {
        // 查询对应weiboid ，是否有对应微博
        WeiboContentEntity weiboContentEntity = weiboContentService.getById(pictureInfoEntity.getWeiboId());
        if(weiboContentEntity == null){
            return R.error("id 为" + pictureInfoEntity.getWeiboId() + "的记录不存在！");
        }
        DateTime date = DateUtil.date();
        pictureInfoEntity.setCreateTime(date);
        pictureInfoEntity.setCreator("admin");
        pictureInfoEntity.setModifiedTime(date);
        pictureInfoEntity.setModifier("admin");
        // 如果有，则先插入图片表，然后再插入中间表 返回 操作成功标志 true
        boolean save = this.save(pictureInfoEntity);
        if(save){
            WeiboPicEntity weiboPicEntity = new WeiboPicEntity();
            weiboPicEntity.setPicId(pictureInfoEntity.getId());
            weiboPicEntity.setWeiboId(pictureInfoEntity.getWeiboId());
            weiboPicEntity.setCreateTime(date);
            weiboPicEntity.setCreator("admin");
            weiboPicEntity.setModifiedTime(date);
            weiboPicEntity.setModifier("admin");
            weiboPicService.save(weiboPicEntity);
        } else {
            return R.error("PictureInfoServiceImpl.saveWeiBoPic:插入失败！");
        }
        return R.ok();
    }

}